package server;

import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.DataInputStream;
import java.io.IOException;

import shared.Vec2f;
import vmo.Chunk;

public class Entity {

	public enum EntityType{WORLD,STATIC,SHIP};
//worlds are planets, they have cores, statics are immobile objects without cores, ships are mobile objects
	//cores mean that A. the world wraps around east to west B. you can't move further south or north than the edge
	//C. they have tiles all the way to the edge of the grid on at least some level
	//on a static or ship moving off the edge sets you adrift in the world
	public EntityType m_type;
	public Chunk [][] m_chunks;
	public short m_width,m_height;
	public float m_mass;
	public Vec2f m_position,m_velocity;
	public boolean m_loaded;
	String m_id; //a name needed to find the thing later
	
	public Entity(Vec2f pos, String ID)
	{
		m_id=ID;
		m_position=pos;
		m_velocity=new Vec2f(0,0);
	}
	
	public Entity(String ID)
	{
		m_id=ID;
	}
	
	public boolean Save() throws IOException
	{
		String str="save/"+m_id+".ent";
		File file=new File(str);
		file.createNewFile();
		
		FileOutputStream fstream;
		DataOutputStream dstream;
		try {
			fstream= new FileOutputStream(file);
			dstream= new DataOutputStream(fstream);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			
			e.printStackTrace();
			return false;
		}
		//write type
		dstream.write(getTypeByte(m_type));
		//write position
		m_position.Save(dstream);
		m_velocity.Save(dstream);
		//write size
		dstream.writeShort(m_width);
		dstream.writeShort(m_height);
		//write chunks
		for (int i=0;i<m_width;i++)
		{
			for (int j=0;j<m_height;j++)
			{
				m_chunks[i][j].Save(dstream);
			}
		}
		return true;
	}
	
	public boolean Load() throws IOException
	{
		String str="save/"+m_id+".ent";
		FileInputStream fstream;
		DataInputStream dstream;	
		try {
			fstream= new FileInputStream(str);
			dstream= new DataInputStream(fstream);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}		
		//read type
		m_type=Entity.getTypefromByte(dstream.readByte());
		//read position
		m_position=new Vec2f(dstream);
		m_velocity=new Vec2f(dstream);
		//read size
		m_width=dstream.readShort();
		m_height=dstream.readShort();
		
		m_chunks=new Chunk[m_width][];
		for (int i=0;i<m_width;i++)
		{
			m_chunks[i]=new Chunk[m_height];
			for (int j=0;j<m_height;j++)
			{
				m_chunks[i][j]=new Chunk(dstream);
			}
		}
		return true;
	}
	
	public static Entity LoadEntity(String string)
	{
		Entity entity=new Entity(string);
		try {
			if (entity.Load())
			{
				return entity;
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return null;
	}
	
	static public byte getTypeByte(EntityType type)
	{
		switch (type)
		{
		case WORLD:
			return 0;
		case STATIC:
			return 1;
		case SHIP:
			return 2;
		}
		return 0;
	}
	
	static public EntityType getTypefromByte(byte b)
	{
		switch (b)
		{
		case 0:
			return EntityType.WORLD;
		case 1:
			return EntityType.STATIC;
		case 2:
			return EntityType.SHIP;
		
		}
		return EntityType.WORLD;
	}
	
	public void WorldGenerator(short width, short height)
	{
		m_type=EntityType.WORLD;
		m_width=width;
		m_height=height;
		//generate grid
		m_chunks=Generators.WorldGenerator0(width, height);
		
		
	}
	
	
}
